What is decamelize-keys?
The `decamelize-keys` npm package is designed to convert object keys from camelCase to lowercase with a customizable separator, making it useful for transforming JavaScript object keys to match different coding or data format conventions.
What are decamelize-keys's main functionalities?
Convert object keys from camelCase to a custom separator
This feature allows you to convert the keys of an object from camelCase to another format using a custom separator, such as a hyphen. It's particularly useful for preparing data to be sent to APIs or systems that require a different naming convention.
const decamelizeKeys = require('decamelize-keys');
const result = decamelizeKeys({ 'fooBar': true, 'barBaz': 'yes' }, '-');
console.log(result); // Output: { 'foo-bar': true, 'bar-baz': 'yes' }
Exclude specific keys from being decamelized
This functionality allows you to exclude certain keys from being transformed. It's useful when you have specific keys that need to remain in camelCase for consistency or compatibility reasons.
const decamelizeKeys = require('decamelize-keys');
const options = { exclude: ['fooBar'] };
const result = decamelizeKeys({ 'fooBar': true, 'barBaz': 'yes' }, '-', options);
console.log(result); // Output: { 'fooBar': true, 'bar-baz': 'yes' }
Other packages similar to decamelize-keys
humps
Similar to `decamelize-keys`, `humps` can also convert keys between camelCase and snake_case. However, `humps` provides more comprehensive options for both decamelizing and camelizing keys, making it a versatile choice for different case conversion needs.
snakeize
The `snakeize` package is focused on converting object keys to snake_case. While it serves a similar purpose to `decamelize-keys` in terms of changing the case of keys, it is specifically tailored for snake_case conversion without the flexibility of choosing a custom separator.
camelcase-keys
As the inverse of `decamelize-keys`, `camelcase-keys` converts object keys from snake_case or kebab-case to camelCase. It's useful for scenarios where you need to convert data to match JavaScript's naming conventions, offering a complementary functionality.
decamelize-keys
Convert object keys from camelCase to lowercase with a custom separator using decamelize
This project was forked from camelcase-keys
and converted to do the opposite
Install
$ npm install --save decamelize-keys
Usage
const decamelizeKeys = require('decamelize-keys');
decamelizeKeys({fooBar: true}, '-');
API
decamelizeKeys(input, [separator], [options])
input
Type: object
Required
Object to decamelize.
separator
Type: string
Default: _
A string to insert between words.
options
Type: object
separator
Type: string
Default: _
Alternative way to specify separator.
exclude
Type: array
Default: []
Exclude keys from being decamelized.
Related
See camelcase-keys
for the inverse.
License
MIT © Sindre Sorhus, Dmirty Sobolev